package com.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.po.Msg;

public class SendSmsUtil {
        public static Msg send1(String phone, String templateCode, String code){
            String accessKeyId="LTAI4GJNCLtgZZpJMH2PWGW3";
            String accessSecret="SJ1oLXClxc07hzWHYUR9QkPLzTLymq";
            //连接阿里云
            //构建一个客户端实例，用于发起请求
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
            IAcsClient client = new DefaultAcsClient(profile);

            //构建请求
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");//不要改，改了会报错
            request.setSysVersion("2017-05-25");//这个日期是官方api的版本号不要动，
            request.setSysAction("SendSms");//这个是api的名称,系统规定参数。取值：SendSms。

            //自定义参数（手机号、验证码、签名、模板）
            request.putQueryParameter("RegionId", "cn-hangzhou");//可以不要，上面有写
            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", "奥德");
            request.putQueryParameter("TemplateCode", templateCode);
            //构建短信验证码
            request.putQueryParameter("TemplateParam",code);
            try {
                // 发起请求，并得到 Response
                CommonResponse response = client.getCommonResponse(request);

                System.out.println(response.getData());
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(response.getData());
                String Code= (String) jsonObject.get("Code");
                System.out.println("Code"+Code);
                if(Code.equals("isv.BUSINESS_LIMIT_CONTROL")){
                    System.out.println("短信发送频繁");
                    return Msg.isv_over();
                }else if(Code.equals("isv.AMOUNT_NOT_ENOUGH")){
                    System.out.println("余额不足，请联系管理员");
                    return Msg.isv_money();
                }else if(Code.equals("OK")){
                    System.out.println("发送成功");
                }
                return Msg.success();
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return Msg.fail();
        }
}
